//指令类型
var Card_CommandType;
(function (Card_CommandType) {
    Card_CommandType[Card_CommandType["OpenReadCardDevice_Type"] = 1] = "OpenReadCardDevice_Type";
    Card_CommandType[Card_CommandType["CloseReadCardDevice_Type"] = 2] = "CloseReadCardDevice_Type";
    Card_CommandType[Card_CommandType["WriteCardData_Type"] = 3] = "WriteCardData_Type";
    Card_CommandType[Card_CommandType["ReadCardData_Type"] = 4] = "ReadCardData_Type";
})(Card_CommandType || (Card_CommandType = {}));
var CNOT_CONNECT = -1;
var Client_Card = /** @class */ (function () {
    function Client_Card() {
        this.url = 'ws://127.0.0.1:8415/aratek';
        this.ws = null;
        this.isConnect = false;
    }
    //发送消息
    Client_Card.prototype.SendMessage = function (msg) {
        if (this.isConnect) {
            this.ws.send(msg);
        }
    };
    //打开设备
    Client_Card.prototype.OpenReadCardDevice = function () {
        if (this.isConnect == false) {
            console.log("Unconnected service");
            return CNOT_CONNECT;
        }
        var cmd_json = {
            "RequestType": Card_CommandType.OpenReadCardDevice_Type
        };
        this.SendMessage(JSON.stringify(cmd_json));
    };
    //关闭设备
    Client_Card.prototype.CloseReadCardDevice = function () {
        if (this.isConnect == false) {
            console.log("Unconnected service");
            return CNOT_CONNECT;
        }
        var cmd_json = {
            "RequestType": Card_CommandType.CloseReadCardDevice_Type
        };
        this.SendMessage(JSON.stringify(cmd_json));
    };
    //写卡
    Client_Card.prototype.WriteCardData = function (data) {
        if (this.isConnect == false) {
            console.log("Unconnected service");
            return CNOT_CONNECT;
        }
        var cmd_json = {
            "RequestType": Card_CommandType.WriteCardData_Type,
            "Data": data,
            "Key": "aaaaaaaaaaaaaaa8"
        };
        this.SendMessage(JSON.stringify(cmd_json));
    };
    //读卡
    Client_Card.prototype.ReadCardData = function () {
        if (this.isConnect == false) {
            console.log("Unconnected service");
            return CNOT_CONNECT;
        }
        var cmd_json = {
            "RequestType": Card_CommandType.ReadCardData_Type,
            "Key": "aaaaaaaaaaaaaaa9"
        };
        this.SendMessage(JSON.stringify(cmd_json));
    };
    //连接助手程序
    Client_Card.prototype.Connect_Server = function () {
        var thisws = this;
        this.ws = new WebSocket(this.url);
        this.ws.onopen = function () {
            alert("Connect Success");
            thisws.isConnect = true;
        };
        //接收返回数据
        this.ws.onmessage = function (ev) {
            var data = JSON.parse(ev.data);
            var responsetype = data.ResponseType;
            switch (responsetype) {
                case Card_CommandType.OpenReadCardDevice_Type:
                    if (thisws.OnOpenReadCardDevice) {
                        thisws.OnOpenReadCardDevice(data.ErrorCode, data.ErrorMessage);
                    }
                    break;
                case Card_CommandType.CloseReadCardDevice_Type:
                    if (thisws.OnCloseReadCardDevice) {
                        thisws.OnCloseReadCardDevice(data.ErrorCode, data.ErrorMessage);
                    }
                    break;
                case Card_CommandType.WriteCardData_Type:
                    if (thisws.OnWriteCardData) {
                        thisws.OnWriteCardData(data.ErrorCode, data.ErrorMessage);
                    }
                    break;
                case Card_CommandType.ReadCardData_Type:
                    if (thisws.OnReadCardData) {
                        thisws.OnReadCardData(data.ErrorCode, data.ErrorMessage, data.Data, data.SN);
                    }
                    break;
                default:
                    if (thisws.OnOtherError) {
                        thisws.OnOtherError(data.ResponseType, data.ErrorCode, data.ErrorMessage);
                    }
                    break;
            }
        };
        this.ws.onclose = function () {
            thisws.isConnect = false;
            console.log("Disconnect");
        };
    };
    Client_Card.prototype.Disconnect_Server = function () {
        var thisws = this;
        this.ws.close();
    };
    return Client_Card;
}());

